<template>
    <div>
        <zh-form ref="fileFieldParamRef" :span="8" :model="pageData.formData" :rules="pageData.rules"  :disabled="props.showFlag" label-width="200px">

           <zh-form-select prop="fieldType" label="字段类型"  allowClear :span="8" :fieldNames="{label: 'dictValue', value: 'dictKey'}" :options="pageData.dictData.FIELDTYPE"
                    v-model="pageData.formData.fieldType"></zh-form-select>
           <zh-form-input prop="fieldKey" label="字段标识" type="text" :span="8" dataType="common"
                    v-model="pageData.formData.fieldKey"></zh-form-input>
           <zh-form-input prop="fieldName" label="字段中文" type="text" :span="8" dataType="common"
                    v-model="pageData.formData.fieldName"></zh-form-input>
            <zh-form-select prop="status" label="有效状态" allowClear :span="8" :fieldNames="{label: 'dictValue', value: 'dictKey'}" :options="pageData.dictData.STATUS"
                    v-model="pageData.formData.status"></zh-form-select>  

        </zh-form>

    </div>
</template>
<script lang="ts" setup name="FileFieldParamForm">
import { ElMessage } from 'element-plus';
//------------------接收传入参数(组件)-------------------
const props = defineProps({
    FileFieldParam: {
        type: Object,
        default: () => { },
        required: false,
    },
    showFlag: {
        type: Boolean,
        default: false
    },
    modelValue: Boolean,
});
//------------------定义子组件---------------------------
const fileFieldParamRef = ref()
const emit = defineEmits(['update:modelValue']);


//------------------内部变量定义-------------------------
const pageData = reactive({
    formData: {
        fieldId:'',
        fieldType:'',
        fieldKey:'',
        fieldName:'',
        status: '',
        createName:'',
        createTime:'',
        updateName:'',
        updateTime:'',
        del:'',
    },
    dictFields: 'STATUS,FIELDTYPE',
    dictData: {} as any,
    rules: reactive({

        fieldType:[
            {
                required:true,
                message:'请输入字段类型',
                trugger:'blur'
            }
        ],
        fieldKey:[
            {
                required:true,
                message:'请输入字段标识',
                trugger:'blur'
            }
        ],
        fieldName:[
            {
                required:true,
                message:'请输入字段中文',
                trugger:'blur'
            }
        ],
        status:[
            {
                required:true,
                message:'请输入有效状态',
                trugger:'blur'
            }
        ],
        

    }),
    editFlag: false,
    codeDisabled: false,
})
//------------------定义调用方法-------------------------
function getDict() {
    setTimeout(() => {
        let params = {
            dictFields: pageData.dictFields,
        };
        $api.common.getBatchDict.post(params).then((res: any) => {
            pageData.dictData = res.result;

        });



    }, 20);
}

function getDetail() {
  
    if (!props.FileFieldParam || !props.FileFieldParam.fieldId) {
        pageData.formData.status='1';
        return
    } else {
        let params = { fieldId: props.FileFieldParam.fieldId };
        $api.common.fileSend.post('fileFieldParam0002', params).then((res) => {
            pageData.codeDisabled = true
            pageData.formData = Object.assign({}, res.result)
        })
    }
}

async function save() {
    //校验表单
    let checkFlag = await fileFieldParamRef.value.checkForm(); 

    if (checkFlag) {
        const info = pageData.formData;
        let ret = { saveType: '', code: 0 };
        if (pageData.codeDisabled) {
            ret = await $api.common.fileSend.post('fileFieldParam0005', info);
            ret.saveType = 'edit';
            if (ret.code === 0) {
                ElMessage.success('修改成功！');
                emit('update:modelValue', false);
            }
        } else {
            ret = await $api.common.fileSend.post('fileFieldParam0004', info);
            ret.saveType = 'add';
            if (ret.code === 0) {
                ElMessage.success('新增成功！');
                emit('update:modelValue', false);
            }
        }
        return ret;
    }
    return null;

}

//------------------初始化------------------------------
onMounted(() => {
    getDict();
    getDetail();
})
if (props.FileFieldParam) {
    if (props.FileFieldParam.fieldId) {
        pageData.codeDisabled = true
    }
}
//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ form: pageData.formData, save });
</script>
<style lang="scss" scoped>

</style>

